// pages/user/order/orderdetail/orderdetail.js
var app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    order: {
      leader_address: '',
      leader_name: '',
      create_time: '',
      items: [],
      order_id: '',
      order_sn: '',
      price: '',
      receiver: '',
      status_str: ''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      'order.order_id': options.id 
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getOrderDetail()
  },

  getOrderDetail: function() {
    let that = this
    wx.showLoading({
      mask: true,
      title: app.globalData.loadText,
    })
    app.getAccessToken(app.getPageUrl(getCurrentPages())).then(function (accessToken) {
      wx.request({
        url: app.globalData.hostAPI + '/api/v1/order/detail?access_token=' + accessToken,
        data: {
          order_id: that.data.order.order_id
        },
        success: res => {
          if(res.data.status === 1) {
            that.setData({
              order: res.data.data
            })
            wx.hideLoading()
          } else {
            wx.showToast({
              icon: 'none',
              title: res.data.msg,
            })
          }
        }
      })
    })
  },

  pay: function() {
    let that = this,
      data
    wx.showLoading({
      mask: true,
      title: app.globalData.loadText,
    })
    app.getAccessToken(app.getPageUrl(getCurrentPages())).then(function (accessToken) {
      wx.request({
        url: app.globalData.hostAPI + '/api/v1/wxpay/unified-order?access_token=' + accessToken,
        data: {
          order_sn: that.data.order.order_sn
        },
        success: res => {
          data = res.data.data
          if (res.data.status === 1) {
            wx.requestPayment({
              timeStamp: data.timeStamp,
              nonceStr: data.nonceStr,
              package: data.package,
              signType: 'MD5',
              paySign: data.paySign,
              success: res => {
                wx.hideLoading()
                wx.showToast({
                  icon: 'none',
                  title: '购买成功',
                })
                wx.navigateTo({
                  url: '../order',
                })
              },
              fail: err => {
                wx.hideLoading()
                wx.showToast({
                  icon: 'none',
                  title: '交易取消',
                })
                wx.navigateTo({
                  url: '../order',
                })
              }
            })
          }
        }
      })
    })
  },

  /**
   * 
   */
  result: function() {
    let that = this
    wx.showModal({
      title: '提示',
      content: '是否确认已提货',
      success: res => {
        if(res.confirm) {
          wx.showLoading({
            mask: true,
            title: app.globalData.loadText,
          })
          app.getAccessToken(app.getPageUrl(getCurrentPages())).then(function (accessToken) {
            wx.request({
              url: app.globalData.hostAPI + '/api/v1/order/confirm-take?access_token=' + accessToken + '&order_id=' + that.data.order.order_id,
              success: res => {
                if (res.data.status === 1) {
                  wx.showToast({
                    icon: 'none',
                    title: '确认收货成功',
                    duration: 3000
                  })
                  wx.navigateTo({
                    url: '../order',
                  })
                } else {
                  wx.showToast({
                    icon: 'none',
                    title: res.data.msg,
                    duration: 3000
                  })
                  wx.navigateTo({
                    url: '../order',
                  })
                }
              }
            })
          })
        }
      }
    })
  },

  cancel: function() {
    let that = this
    wx.showModal({
      title: '提示',
      content: '是否取消订单',
      success: res => {
        if(res.confirm) {
          wx.showLoading({
            mask: true,
            title: app.globalData.loadText,
          })
          app.getAccessToken(app.getPageUrl(getCurrentPages())).then(function (accessToken) {
            wx.request({
              url: app.globalData.hostAPI + '/api/v1/order/cancel?access_token=' + accessToken + '&order_id=' + that.data.order.order_id,
              success: res => {
                if (res.data.status === 1) {
                  wx.showToast({
                    icon: 'none',
                    title: '取消订单成功',
                    success: res => {
                      wx.hideLoading()
                      wx.navigateTo({
                        url: './order',
                      })
                    }
                  })
                }
              }
            })
          })
        }
      }
    })
  },

  /**
   * 退款
   */
  refund: function(e) {
    let that = this,
        refund = {
          order_id: that.data.order.order_id,
          item: e.currentTarget.dataset.item
        }
    wx.navigateTo({
      url: '../../service/detail/detail?refund=' + JSON.stringify(refund),
    })
  }
})